extends CenterContainer


onready var z_op = $PanelContainer/VBoxContainer/z/OptionButton
onready var item_op = $PanelContainer/VBoxContainer/item/ItemSelect
onready var num_input = $PanelContainer/VBoxContainer/num/SpinBox
onready var visible_op = $PanelContainer/VBoxContainer/visible/OptionButton
onready var day_time_input = $PanelContainer/VBoxContainer/day_time/SpinBox
onready var res_input = $PanelContainer/VBoxContainer/res/LineEdit
onready var desc_input = $PanelContainer/VBoxContainer/desc/LineEdit
var edit_data = null


func set_edit_data(data):
	edit_data = data
	
	var map_view = MainTool.getUILayer().get_ui("MapView")
	var position_info = map_view.get_select_position_info()
	var door_list = DataSource.get_data_list(DataSource.map_door)
			
	if edit_data:
		$PanelContainer/VBoxContainer/position/position.text = str("(",data.x, " ", data.y, ")")
		z_op.select(z_op.get_item_index(data["z"]))
		
		item_op.set_value(edit_data["itemId"])
		
		visible_op.select(visible_op.get_item_index(data["visible"]))
		
		num_input.value = data["count"]
		day_time_input.value = data["dayTime"]
		res_input.text = data.get("res", "")
		desc_input.text = data.get("description","")
		$PanelContainer/VBoxContainer/modalTitle.text = "编辑地图道具"
		
		
	else:
		pass
		

func state_update():
	var map_view = MainTool.getUILayer().get_ui("MapView")
	
	# 更新当前坐标
	var position_info = map_view.get_select_position_info()
	$PanelContainer/VBoxContainer/position/position.text = str("(",position_info.x, " ", position_info.y, ")")
	

func _on_ok_button_up():
	var map_view = MainTool.getUILayer().get_ui("MapView")
	
	var position_info = map_view.get_select_position_info()
	
	
	
	if edit_data:
		read_form(edit_data)
		
		DataSource.update_data(DataSource.map_prop, edit_data)
		
		MainTool.toast_info("更新道具成功!")
	else:
		var data = {
			"x": position_info.x,
			"y": position_info.y,
			"mapId": position_info["map_register"]["id"]
		}
		
		
		read_form(data)
		
	
		var pre_list = DataSource.get_data_by_list(DataSource.map_prop, position_info["map_register"]["id"], "mapId")
		for pre in pre_list:
			if pre["x"] == position_info.x and pre["y"] == position_info.y:
				MainTool.toast_info("该位置不可重复添加!")
				return
	
		DataSource.add_data(DataSource.map_prop, data)
		
		MainTool.toast_info("添加道具成功!")
	
	map_view.entity_update()
	
	MainTool.getUILayer().pop_ui()


func read_form(dic):
	dic["z"] = z_op.get_selected_id()
	dic["itemId"] = item_op.get_value()
	dic["visible"] = visible_op.get_selected_id()
	dic["count"] = num_input.value
	dic["dayTime"] = day_time_input.value
	dic["res"] = res_input.text
	dic["description"] = desc_input.text
	if dic["dayTime"] <= 0:
		dic["repeat"] = 0
	else:
		dic["repeat"] = 1
	
func _on_cancel_button_up():
	MainTool.getUILayer().pop_ui()
